package com.foreveredu.propms.persist.hibernate;

import java.util.List;

import com.foreveredu.propms.domain.Bill;
import com.foreveredu.propms.domain.Building;
import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.ChargeRecord;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.persist.IBuildingDao;
import com.foreveredu.propms.persist.IChargeItemDao;
import com.foreveredu.propms.persist.IChargeRecordDao;

public class HibernateChargeRecordDao extends HibernateAbstractDao<ChargeRecord, Integer> implements IChargeRecordDao {

	private static final long serialVersionUID = 1L;
	
	public HibernateChargeRecordDao() {
		super(ChargeRecord.class);
	}

	@Override
	public void countChargeRecordForHouseAndDate(House house, int year,
			int month) {
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChargeRecord> findChargeRecordByHouseAndDate(House house,
			int year, int month) {
		String hql = "from ChargeRecord cr where cr.house.id=? and cr.year=? and cr.month=?";
		List<ChargeRecord> crList = this.getHibernateTemplate().find(hql, new Object[]{house.getId(),year,month});
		
		return crList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChargeRecord> findByBill(Bill bill) {
		String hql = "from ChargeRecord cr where cr.bill.id=? order by cr.chargeItem.id";
		List<ChargeRecord> crList = this.getHibernateTemplate().find(hql, bill.getId());
		
		return crList;

	}

	@SuppressWarnings("unchecked")
	@Override
	public ChargeRecord findByHouseAndChargeItem(House house, ChargeItem item) {
		ChargeRecord cr = null;
		
		String hql = "from ChargeRecord cr where cr.bill.house.id=? and cr.chargeItem.id=?";
		List<ChargeRecord> crList = this.getHibernateTemplate().find(hql, new Object[]{house.getId(), item.getId()});
		
		if(crList != null && crList.size()>0) {
			cr = crList.get(0);
		}
		
		return cr;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChargeRecord> findByHouseAndChargeItemInYear(House house,
			ChargeItem item, int year) {
		String hql = "from ChargeRecord cr where cr.bill.house.id=? and cr.chargeItem.id=? and cr.bill.year=?";
		List<ChargeRecord> crList = this.getHibernateTemplate().find(hql, new Object[]{house.getId(), item.getId(), year});
		if(crList!=null && crList.size()>0) {
			return crList;
		} else {
			return null;
		}
	}

}
